/*
Copyright 2019 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mysql

import (
	"reflect"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"vitess.io/vitess/go/mysql/replication"
)

// sample event data
var (
	mariadbFormatEvent         = []byte{0x87, 0x41, 0x9, 0x54, 0xf, 0x88, 0xf3, 0x0, 0x0, 0xf4, 0x0, 0x0, 0x0, 0xf8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x31, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x33, 0x2d, 0x4d, 0x61, 0x72, 0x69, 0x61, 0x44, 0x42, 0x2d, 0x31, 0x7e, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x65, 0x2d, 0x6c, 0x6f, 0x67, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x87, 0x41, 0x9, 0x54, 0x13, 0x38, 0xd, 0x0, 0x8, 0x0, 0x12, 0x0, 0x4, 0x4, 0x4, 0x4, 0x12, 0x0, 0x0, 0xdc, 0x0, 0x4, 0x1a, 0x8, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x2, 0x0, 0x0, 0x0, 0xa, 0xa, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0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x0, 0x0, 0x0, 0x79, 0xa, 0x0, 0x0, 0x0, 0x0, 0x27, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x1a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x21, 0x0, 0x21, 0x0, 0x21, 0x0, 0x76, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6b, 0x65, 0x79, 0x73, 0x70, 0x61, 0x63, 0x65, 0x0, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x76, 0x74, 0x5f, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x28, 0x6d, 0x73, 0x67, 0x29, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x20, 0x28, 0x27, 0x74, 0x65, 0x73, 0x74, 0x20, 0x30, 0x27, 0x29, 0x20, 0x2f, 0x2a, 0x20, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x20, 0x76, 0x74, 0x5f, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x20, 0x28, 0x69, 0x64, 0x20, 0x29, 0x20, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x29, 0x3b, 0x20, 0x2a, 0x2f}

	mariadbChecksumFormatEvent        = []byte{0x22, 0xe5, 0x3e, 0x54, 0xf, 0x8b, 0xf3, 0x0, 0x0, 0xf4, 0x0, 0x0, 0x0, 0xf8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x31, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x33, 0x2d, 0x4d, 0x61, 0x72, 0x69, 0x61, 0x44, 0x42,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x1, 0x14, 0x13, 0x32, 0xdc}
	mariadbChecksumQueryEvent         = []byte{0x22, 0xe5, 0x3e, 0x54, 0x2, 0x8a, 0xf3, 0x0, 0x0, 0xd9, 0x0, 0x0, 0x0, 0x69, 0x2, 0x0, 0x0, 0x0, 0x0, 0x1d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x1a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x8, 0x0, 0x8, 0x0, 0x21, 0x0, 0x76, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6b, 0x65, 0x79, 0x73, 0x70, 0x61, 0x63, 0x65, 0x0, 0x55, 0x50, 0x44, 0x41, 0x54, 0x45, 0x20, 0x5f, 0x76, 0x74, 0x2e, 0x62, 0x6c, 0x70, 0x5f, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x20, 0x53, 0x45, 0x54, 0x20, 0x70, 0x6f, 0x73, 0x3d, 0x27, 0x4d, 0x61, 0x72, 0x69, 0x61, 0x44, 0x42, 0x2f, 0x30, 0x2d, 0x36, 0x32, 0x33, 0x34, 0x34, 0x2d, 0x31, 0x34, 0x27, 0x2c, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x3d, 0x31, 0x34, 0x31, 0x33, 0x34, 0x30, 0x38, 0x30, 0x33, 0x34, 0x2c, 0x20, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x3d, 0x31, 0x34, 0x31, 0x33, 0x34, 0x30, 0x38, 0x30, 0x33, 0x34, 0x20, 0x57, 0x48, 0x45, 0x52, 0x45, 0x20, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x5f, 0x73, 0x68, 0x61, 0x72, 0x64, 0x5f, 0x75, 0x69, 0x64, 0x3d, 0x30, 0xce, 0x49, 0x7a, 0x53}
	mariadbChecksumStrippedQueryEvent = []byte{0x22, 0xe5, 0x3e, 0x54, 0x2, 0x8a, 0xf3, 0x0, 0x0, 0xd9, 0x0, 0x0, 0x0, 0x69, 0x2, 0x0, 0x0, 0x0, 0x0, 0x1d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x1a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x8, 0x0, 0x8, 0x0, 0x21, 0x0, 0x76, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6b, 0x65, 0x79, 0x73, 0x70, 0x61, 0x63, 0x65, 0x0, 0x55, 0x50, 0x44, 0x41, 0x54, 0x45, 0x20, 0x5f, 0x76, 0x74, 0x2e, 0x62, 0x6c, 0x70, 0x5f, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x20, 0x53, 0x45, 0x54, 0x20, 0x70, 0x6f, 0x73, 0x3d, 0x27, 0x4d, 0x61, 0x72, 0x69, 0x61, 0x44, 0x42, 0x2f, 0x30, 0x2d, 0x36, 0x32, 0x33, 0x34, 0x34, 0x2d, 0x31, 0x34, 0x27, 0x2c, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x3d, 0x31, 0x34, 0x31, 0x33, 0x34, 0x30, 0x38, 0x30, 0x33, 0x34, 0x2c, 0x20, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x3d, 0x31, 0x34, 0x31, 0x33, 0x34, 0x30, 0x38, 0x30, 0x33, 0x34, 0x20, 0x57, 0x48, 0x45, 0x52, 0x45, 0x20, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x5f, 0x73, 0x68, 0x61, 0x72, 0x64, 0x5f, 0x75, 0x69, 0x64, 0x3d, 0x30}

	mariadbSemiSyncNoAckInsertEvent = []byte{0xef, 0x00, 0x88, 0x41, 0x9, 0x54, 0x2, 0x88, 0xf3, 0x0, 0x0, 0xa8, 0x0, 0x0, 0x0, 0x79, 0xa, 0x0, 0x0, 0x0, 0x0, 0x27, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x1a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x21, 0x0, 0x21, 0x0, 0x21, 0x0, 0x76, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6b, 0x65, 0x79, 0x73, 0x70, 0x61, 0x63, 0x65, 0x0, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x76, 0x74, 0x5f, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x28, 0x6d, 0x73, 0x67, 0x29, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x20, 0x28, 0x27, 0x74, 0x65, 0x73, 0x74, 0x20, 0x30, 0x27, 0x29, 0x20, 0x2f, 0x2a, 0x20, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x20, 0x76, 0x74, 0x5f, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x20, 0x28, 0x69, 0x64, 0x20, 0x29, 0x20, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x29, 0x3b, 0x20, 0x2a, 0x2f}
	mariadbSemiSyncAckInsertEvent   = []byte{0xef, 0x01, 0x88, 0x41, 0x9, 0x54, 0x2, 0x88, 0xf3, 0x0, 0x0, 0xa8, 0x0, 0x0, 0x0, 0x79, 0xa, 0x0, 0x0, 0x0, 0x0, 0x27, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x1a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x21, 0x0, 0x21, 0x0, 0x21, 0x0, 0x76, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6b, 0x65, 0x79, 0x73, 0x70, 0x61, 0x63, 0x65, 0x0, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x76, 0x74, 0x5f, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x28, 0x6d, 0x73, 0x67, 0x29, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x20, 0x28, 0x27, 0x74, 0x65, 0x73, 0x74, 0x20, 0x30, 0x27, 0x29, 0x20, 0x2f, 0x2a, 0x20, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x20, 0x76, 0x74, 0x5f, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x20, 0x28, 0x69, 0x64, 0x20, 0x29, 0x20, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x29, 0x3b, 0x20, 0x2a, 0x2f}
)

func TestMariadbStandaloneGTIDEventIsGTID(t *testing.T) {
	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbStandaloneGTIDEvent)}
	want := true
	if got := input.IsGTID(); got != want {
		t.Errorf("%#v.IsGTID() = %v, want %v", input, got, want)
	}
}

func TestMariadbBeginGTIDEventIsGTID(t *testing.T) {
	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbBeginGTIDEvent)}
	want := true
	if got := input.IsGTID(); got != want {
		t.Errorf("%#v.IsGTID() = %v, want %v", input, got, want)
	}
}

func TestMariadbBinlogEventIsntGTID(t *testing.T) {
	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbInsertEvent)}
	want := false
	if got := input.IsGTID(); got != want {
		t.Errorf("%#v.IsGTID() = %v, want %v", input, got, want)
	}
}

func TestMariadbNotBeginGTID(t *testing.T) {
	f, err := binlogEvent(mariadbFormatEvent).Format()
	if err != nil {
		t.Errorf("unexpected error: %v", err)
		return
	}

	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbStandaloneGTIDEvent)}
	want := false
	if _, got, err := input.GTID(f); got != want {
		t.Errorf("%#v.GTID() = %v (%v), want %v", input, got, err, want)
	}
}

func TestMariadbIsBeginGTID(t *testing.T) {
	f, err := binlogEvent(mariadbFormatEvent).Format()
	if err != nil {
		t.Errorf("unexpected error: %v", err)
		return
	}

	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbBeginGTIDEvent)}
	want := true
	if _, got, err := input.GTID(f); got != want {
		t.Errorf("%#v.IsBeginGTID() = %v (%v), want %v", input, got, err, want)
	}
}

func TestMariadbStandaloneBinlogEventGTID(t *testing.T) {
	f, err := binlogEvent(mariadbFormatEvent).Format()
	if err != nil {
		t.Errorf("unexpected error: %v", err)
		return
	}

	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbStandaloneGTIDEvent)}
	want := replication.MariadbGTID{Domain: 0, Server: 62344, Sequence: 9}
	got, hasBegin, err := input.GTID(f)
	assert.NoError(t, err, "unexpected error: %v", err)
	assert.False(t, hasBegin, "unexpected hasBegin")
	assert.True(t, reflect.DeepEqual(got, want), "%#v.GTID() = %#v, want %#v", input, got, want)

}

func TestMariadbBinlogEventGTID(t *testing.T) {
	f, err := binlogEvent(mariadbFormatEvent).Format()
	if err != nil {
		t.Errorf("unexpected error: %v", err)
		return
	}

	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbBeginGTIDEvent)}
	want := replication.MariadbGTID{Domain: 0, Server: 62344, Sequence: 10}
	got, hasBegin, err := input.GTID(f)
	assert.NoError(t, err, "unexpected error: %v", err)
	assert.True(t, hasBegin, "unexpected !hasBegin")
	assert.True(t, reflect.DeepEqual(got, want), "%#v.GTID() = %#v, want %#v", input, got, want)

}

func TestMariadbBinlogEventFormat(t *testing.T) {
	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbFormatEvent)}
	want := BinlogFormat{
		FormatVersion:     4,
		ServerVersion:     "10.0.13-MariaDB-1~precise-log",
		HeaderLength:      19,
		ChecksumAlgorithm: 0,
		HeaderSizes:       mariadbFormatEvent[76 : len(mariadbFormatEvent)-5],
	}
	got, err := input.Format()
	assert.NoError(t, err, "unexpected error: %v", err)
	assert.True(t, reflect.DeepEqual(got, want), "%#v.Format() = %v, want %v", input, got, want)

}

func TestMariadbBinlogEventChecksumFormat(t *testing.T) {
	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbChecksumFormatEvent)}
	want := BinlogFormat{
		FormatVersion:     4,
		ServerVersion:     "10.0.13-MariaDB-1~precise-log",
		HeaderLength:      19,
		ChecksumAlgorithm: 1,
		HeaderSizes:       mariadbChecksumFormatEvent[76 : len(mariadbChecksumFormatEvent)-5],
	}
	got, err := input.Format()
	assert.NoError(t, err, "unexpected error: %v", err)
	assert.True(t, reflect.DeepEqual(got, want), "%#v.Format() = %v, want %v", input, got, want)

}

func TestMariadbBinlogEventStripChecksum(t *testing.T) {
	f, err := (mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbChecksumFormatEvent)}).Format()
	require.NoError(t, err, "unexpected error: %v", err)

	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbChecksumQueryEvent)}
	wantEvent := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbChecksumStrippedQueryEvent)}
	wantChecksum := []byte{0xce, 0x49, 0x7a, 0x53}
	gotEvent, gotChecksum, err := input.StripChecksum(f)
	require.NoError(t, err, "unexpected error: %v", err)

	if !reflect.DeepEqual(gotEvent, wantEvent) || !reflect.DeepEqual(gotChecksum, wantChecksum) {
		t.Errorf("%#v.StripChecksum() = (%v, %v), want (%v, %v)", input, gotEvent, gotChecksum, wantEvent, wantChecksum)
	}
}

func TestMariadbBinlogEventStripChecksumNone(t *testing.T) {
	f, err := (mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbFormatEvent)}).Format()
	require.NoError(t, err, "unexpected error: %v", err)

	input := mariadbBinlogEvent{binlogEvent: binlogEvent(mariadbStandaloneGTIDEvent)}
	want := input
	gotEvent, gotChecksum, err := input.StripChecksum(f)
	require.NoError(t, err, "unexpected error: %v", err)

	if !reflect.DeepEqual(gotEvent, want) || gotChecksum != nil {
		t.Errorf("%#v.StripChecksum() = (%v, %v), want (%v, nil)", input, gotEvent, gotChecksum, want)
	}
}

func TestMariaDBSemiSyncAck(t *testing.T) {
	{
		c := Conn{ExpectSemiSyncIndicator: false}
		buf, semiSyncAckRequested, err := c.AnalyzeSemiSyncAckRequest(mariadbInsertEvent)
		assert.NoError(t, err)
		e := NewMariadbBinlogEventWithSemiSyncInfo(buf, semiSyncAckRequested)

		assert.False(t, e.IsSemiSyncAckRequested())
		assert.True(t, e.IsQuery())
	}
	{
		c := Conn{ExpectSemiSyncIndicator: true}
		buf, semiSyncAckRequested, err := c.AnalyzeSemiSyncAckRequest(mariadbSemiSyncNoAckInsertEvent)
		assert.NoError(t, err)
		e := NewMariadbBinlogEventWithSemiSyncInfo(buf, semiSyncAckRequested)

		assert.False(t, e.IsSemiSyncAckRequested())
		assert.True(t, e.IsQuery())
	}
	{
		c := Conn{ExpectSemiSyncIndicator: true}
		buf, semiSyncAckRequested, err := c.AnalyzeSemiSyncAckRequest(mariadbSemiSyncAckInsertEvent)
		assert.NoError(t, err)
		e := NewMariadbBinlogEventWithSemiSyncInfo(buf, semiSyncAckRequested)

		assert.True(t, e.IsSemiSyncAckRequested())
		assert.True(t, e.IsQuery())
	}
}
